package com.itheima.controller;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;

import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    @Reference
    private RoleService roleService;


    //权限分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = permissionService.pageQuery(queryPageBean);
        return pageResult;
    }

    //新增权限
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission, Integer[] menuIds) {
        try {
            permissionService.add(permission, menuIds);
            return new Result(true, "新增权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增权限失败");
        }
    }

    //编辑权限
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission, Integer[] menuIds) {
        try {
            permissionService.edit(permission, menuIds);
            return new Result(true, "修改权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改权限失败");
        }
    }

    //查询权限
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Permission permission = permissionService.findById(id);
            return new Result(true, "查询权限成功", permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询权限失败");
        }
    }

    //删除权限
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            permissionService.deleteById(id);
            return new Result(true, "删除权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除权限失败");
        }
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Permission> list = permissionService.findAll();
            return new Result(true, "查询所有权限成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询所有权限失败");
        }
    }

    @RequestMapping("/findMenuIdsByPermissionId")
    public Result findMenuIdsByPermissionId(Integer id) {
        try {
            List<Menu> list = permissionService.findMenuIdsByPermissionId(id);
            return new Result(true, "查询菜单成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询菜单失败");
        }
    }

    @RequestMapping("/findpermissionidsbyroleid")
    public Result findpermissionidsbyroleid(Integer roleId) {
        List<Integer> ids = permissionService.findpermissionidsbyroleid(roleId);
        return new Result(true, "查询成功", ids);
    }
}
